package holder;

import java.io.File;

import manager.DownLoadInfo;
import manager.DownLoadManager;
import manager.DownLoadManager.DownloadInfoObserver;

import com.example.googlemarketdemo.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import conf.constants.URLS;

import utils.BitmapHepler;
import utils.CommonUtils;
import utils.PrintDownLoadInfo;
import utils.StringUtils;
import utils.UIUtils;
import view.ProgressView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import base.BaseHolder;
import bean.AppInfoBean;

/**
 * 
 * @author Anson-pc
 *
 */
public class ItemHolder extends BaseHolder<AppInfoBean> implements DownloadInfoObserver, OnClickListener{

	@ViewInject(R.id.item_appinfo_iv_icon)
	ImageView	mIvIcon;

	@ViewInject(R.id.item_appinfo_rb_stars)
	RatingBar	mRbStars;

	@ViewInject(R.id.item_appinfo_tv_des)
	TextView	mTvDes;

	@ViewInject(R.id.item_appinfo_tv_size)
	TextView	mTvSize;

	@ViewInject(R.id.item_appinfo_tv_title)
	TextView	mTvTitle;
	
	@ViewInject(R.id.item_appinfo_pv)
	ProgressView	mProgressView;

	private AppInfoBean mAppInfoBean;
	
	
	@Override
	protected View initHolderView() {
		View rootView = View.inflate(UIUtils.getContext(), R.layout.item_home_info, null);
		// 找孩子
		ViewUtils.inject(this, rootView);
		//设置点击事件
		mProgressView.setOnClickListener(this);
		return rootView;
	}

	@Override
	protected void refreshHolderView(AppInfoBean data) {
		/**
		 * 因为只有在正在下载的时候，mProgressView才会设置
		 */
		//重置
		mProgressView.setProgress(0);
		
		
		mAppInfoBean = data;
		mTvDes.setText(data.des);
		mTvSize.setText(StringUtils.formatFileSize(data.size));
		mTvTitle.setText(data.name);

		mRbStars.setRating(data.stars);

		// 默认图片
		mIvIcon.setImageResource(R.drawable.ic_default);
		// 图片加载
		// BitmapUtils bitmapUtils = new BitmapUtils(UIUtils.getContext());
		// http://localhost:8080/GooglePlayServer/image?name=app/com.itheima.www/icon.jpg
		BitmapHepler.display(mIvIcon, URLS.IMAGEBASEURL + data.iconUrl);
		
		
		/*-----------  根据不同的状态给用户提示 ----------*/
		
		/**
		 状态(编程记录)  	|  给用户的提示(ui展现) 
		----------------|-----------------------	
		未下载			|下载				    	
		下载中			|显示进度条		   			
		暂停下载			|继续下载				
		等待下载			|等待中...				
		下载失败 			|重试						
		下载完成 			|安装						
		已安装 			|打开						

		 */
		// 状态在哪里？--》DownloadInfo 里面
		DownLoadInfo downLoadInfo = DownLoadManager.getInstance()
				.getDownLoadInfo(mAppInfoBean);
		/*
		 * public static final int STATE_UNDOWNLOAD = 0; public static final int
		 * STATE_DOWNLOADING = 1; public static final int STATE_PAUSEDOWNLOAD =
		 * 2; public static final int STATE_WAITINGDOWNLOAD = 3; public static
		 * final int STATE_DOWNLOADFAILED = 4; public static final int
		 * STATE_DOWNLOADED = 5; public static final int STATE_INSTALLED = 6;
		 */
		refreshProgressView(downLoadInfo);
	}

	/**
	 *  刷新按钮的UI
	 * @param downLoadInfo
	 */
	public void refreshProgressView(DownLoadInfo downLoadInfo) {
		
		switch (downLoadInfo.curState) {
		case DownLoadManager.STATE_UNDOWNLOAD:// 未下载
			mProgressView.setNote("下载");
			mProgressView.setIcon(R.drawable.ic_download);
			break;
		case DownLoadManager.STATE_DOWNLOADING:// 下载中
			mProgressView.setIcon(R.drawable.ic_pause);
			mProgressView.setMax(downLoadInfo.max);
			mProgressView.setProgress(downLoadInfo.progress);
			int progress = (int) (downLoadInfo.progress *100.0f/downLoadInfo.max+.5f);
			mProgressView.setNote(progress+"%");
			break;
		case DownLoadManager.STATE_PAUSEDOWNLOAD:// 暂停下载
			mProgressView.setNote("继续下载");
			mProgressView.setIcon(R.drawable.ic_resume);
			break;
		case DownLoadManager.STATE_WAITINGDOWNLOAD:// 等待下载
			mProgressView.setNote("等待中。。。");
			mProgressView.setIcon(R.drawable.ic_pause);
			break;
		case DownLoadManager.STATE_DOWNLOADFAILED:// 下载失败
			mProgressView.setNote("重试");
			mProgressView.setIcon(R.drawable.ic_redownload);
			break;
		case DownLoadManager.STATE_DOWNLOADED:// 下载完成
			mProgressView.setProgressEnable(false);
			mProgressView.setNote("安装");
			mProgressView.setIcon(R.drawable.ic_install);
			break;
		case DownLoadManager.STATE_INSTALLED:// 安装完成
			mProgressView.setNote("打开");
			mProgressView.setIcon(R.drawable.ic_install);
			break;

		default:
			break;
		}
	}
	
	
	/**
	 * 接收数据的改变
	 */
	@Override
	public void onDownLoadInfoChange(final DownLoadInfo downLoadInfo) {
		//过滤，只有能显示对应的Holder才能显示下载进度，根据包名
				if (!downLoadInfo.packageName.equals(mAppInfoBean.packageName)) {
					return ;//退出当前onDownLoadInfoChange的方法
				}
				
				//打印状态
				PrintDownLoadInfo.printDownLoadInfo(downLoadInfo);
				UIUtils.postTaskSafely(new Runnable() {
					
					@Override
					public void run() {
						
						//更新Progressview的UI
						refreshProgressView(downLoadInfo);
					}
				});
	}

	@Override
	public void onClick(View v) {
		/*-----------  根据不同的状态触发不同的操作 ----------*/
		DownLoadInfo downLoadInfo = DownLoadManager.getInstance()
				.getDownLoadInfo(mAppInfoBean);
		switch (downLoadInfo.curState) {
		case DownLoadManager.STATE_UNDOWNLOAD:// 未下载
			// 执行下载
			doDownLoad(downLoadInfo);
			break;
		case DownLoadManager.STATE_DOWNLOADING:// 下载中
			// 暂停下载
			pauseDownLoad(downLoadInfo);
			break;
		case DownLoadManager.STATE_PAUSEDOWNLOAD:// 暂停下载
			doDownLoad(downLoadInfo);
			break;
		case DownLoadManager.STATE_WAITINGDOWNLOAD:// 等待下载
			cancelDownLoad(downLoadInfo);
			break;
		case DownLoadManager.STATE_DOWNLOADFAILED:// 下载失败
			doDownLoad(downLoadInfo);
			break;
		case DownLoadManager.STATE_DOWNLOADED:// 下载完成
			installApk(downLoadInfo);
			break;
		case DownLoadManager.STATE_INSTALLED:// 安装完成
			openApk(downLoadInfo);
			break;

		default:
			break;
		}
	}
	
	/**
	 * 打开apk
	 * @param downLoadInfo
	 */
	private void openApk(DownLoadInfo downLoadInfo) {
		CommonUtils.openApp(UIUtils.getContext(), downLoadInfo.packageName);

	}

	/**
	 * 安装apk
	 * @param downLoadInfo
	 */
	private void installApk(DownLoadInfo downLoadInfo) {

		File apkFile = new File(downLoadInfo.savePath);
		CommonUtils.installApp(UIUtils.getContext(), apkFile);
	}

	/**
	 * 取消下载
	 * @param downLoadInfo
	 */
	private void cancelDownLoad(DownLoadInfo downLoadInfo) {
		// TODO Auto-generated method stub

	}

	/**
	 * 暂停下载
	 * @param downLoadInfo
	 */
	private void pauseDownLoad(DownLoadInfo downLoadInfo) {

		DownLoadManager.getInstance().pauseDownLoad(downLoadInfo);
	}

	/**
	 * 开始下载，断点下载，取消操作
	 * @param downLoadInfo
	 */
	private void doDownLoad(DownLoadInfo downLoadInfo) {
		 // 得到下载管理器
		 DownLoadManager.getInstance().download(downLoadInfo);

		
	}

}
